package com.dengc.springboot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dengc.springboot.common.Constants;
import com.dengc.springboot.controller.dto.UserDTO;
import com.dengc.springboot.entity.Menu;
import com.dengc.springboot.entity.User;
import com.dengc.springboot.exception.ServiceException;
import com.dengc.springboot.mapper.RoleMapper;
import com.dengc.springboot.mapper.RoleMenuMapper;
import com.dengc.springboot.mapper.UserMapper;
import com.dengc.springboot.service.IMeunService;
import com.dengc.springboot.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dengc.springboot.utils.TokenUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dengc
 * @since 2023-03-28
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Resource
    private IMeunService meunService;

    private static final Log LOG = Log.get();
    @Override
    public UserDTO login(UserDTO userDTO) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",userDTO.getUsername());
        queryWrapper.eq("password",userDTO.getPassword());
        User one = getOne(queryWrapper);
        //密码错误时使用这个抛出异常
        if (one != null){
            BeanUtil.copyProperties(one,userDTO,true);
            //设置token
            String token = TokenUtils.genToken(one.getId().toString(), one.getPassword());
            userDTO.setToken(token);
            String role = one.getRole();
            //设置用户的菜单列表
            List<Menu> roleMenus = getRoleMenus(role);
            userDTO.setMeuns(roleMenus);
            return userDTO;
        }else{
            throw new  ServiceException(Constants.CODE_600,"用户名或密码错误");
        }
    }

    //实现注册
    @Override
    public Boolean register(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",user.getUsername());
        User one = getOne(queryWrapper);
        //为空说明数据库中不存在相同用户名的账号可以进行注册
        if(one == null){
            one = new User();
            BeanUtil.copyProperties(user,one,true);
            save(one);
            System.out.println(one.getPassword());
            return true;
        }else {
            throw new  ServiceException(Constants.CODE_500,"用户名已存在");
        }
    }

    /**
     *
     * 获取当前角色的菜单列表
     * **/
    private List<Menu> getRoleMenus(String roleFlag){
        Integer roleId = roleMapper.selectByFlag(roleFlag);
        //当前角色的所有菜单id集合
        List<Integer> menuIds = roleMenuMapper.selectByRoleId(roleId);
        //查出系统所有菜单
        List<Menu> menus = meunService.findMenus("");
        //筛选完成的list
        List<Menu> roleMenus = new ArrayList<>();
        //筛选当前用户角色菜单
        for (Menu meun : menus){
            if (menuIds.contains(meun.getId())){
                roleMenus.add(meun);
            }
            List<Menu> children = meun.getChildren();
            //移出子菜单中不在ids集合的元素
            children.removeIf(child -> !menuIds.contains(child.getId()));
        }
        return  roleMenus;
    }
}
